package com.rzy.f_lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author gao
 * @time 2020/05/25 23:57:32
 * tryLock
 */
public class App5 {
    static class T implements Runnable{
        private Lock lock = new ReentrantLock();
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "开始执行");
            boolean b = lock.tryLock();  // 该方法压根不阻塞！
            if(b) {
                System.out.println(Thread.currentThread().getName() + "拿到锁");
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lock.unlock();
                System.out.println(Thread.currentThread().getName() + "释放锁");
            }

            System.out.println(Thread.currentThread().getName() + "干别的事情去了");


         }
    }
    public static void main(String[] args) throws InterruptedException {
        T t = new T();
        Thread th = new Thread(t);
        Thread th2 = new Thread(t);
        th.start();
        TimeUnit.SECONDS.sleep(1);
        th2.start();

    }
}
